
<html>
<head>
<style type="text/css">

table{	
border-collapse:collapse;	
}
tr.tables
{
border:1px solid darkgrey;
background-color:black;
color:white;
}

tr.attributes
{
border:1px solid white;
background-color:#303030;
color:white;
}
tr.rowa
{
border:1px solid white;
background-color:#D0D0D0;
color:black;
}
tr.rowb
{
border:1px solid white;
background-color:white;
color:black;
}
</style>



</head>
<body>

<?php

print "<div align=center><form action=search.php method=post>";
print "<input type=text maxlength=100 name=query size='150' value=\"". htmlspecialchars($raw_query)."\"></input>";
print "<input type=submit  value=Run></input></form><br>";

//SOuRCE FOR LINKS HERE
print "<div align=center><table width=800>
		<tr class=rowb>
		<td><a href=addmovie.php><b>Add Movie</b></a></td>
		<td><a href=addmovieactor.php><b>Add Movie Actor</b></a></td>
		<td><a href=addmoviedirector.php><b>Add Movie Director</b></a></td>
		<td><a href=add_actor_director.php><b>Add Actor or Director</b></a>	</td>
		</tr>
		</table><br><br></div>";
		

function connect_to_server(){
	$DB="CS143";
	$db_connection = mysql_connect("localhost", "cs143", "");
	if(!$db_connection) {
		$errmsg = mysql_error($db_connection);
		print "Connection failed: " .$errmsg ."<br />";
		exit(1);
	}
	mysql_select_db("$DB", $db_connection);
	return $db_connection;

}

function make_query($command, $db_connection){
		
	$resTable = mysql_query($command, $db_connection);
	if(!$resTable) {
		$errmsg = mysql_error($db_connection);

		if($command != ""){
			print "Query fetch failed: " .$errmsg ."<br />";
		}

		exit(1);
	
	}
	return $resTable;
	
}

$data["directors"] = $_POST["directors"];
$data["movies"] = $_POST["movies"];

$conn=connect_to_server();
$getDirectorsQuery = "SELECT first,last,id FROM Director;";
$getMoviesQuery = "SELECT DISTINCT title,id FROM Movie;";


if($_SERVER['REQUEST_METHOD'] ==  'POST' ){
		$mid = $_POST["mid"];
		$did = $_POST["did"];
}
else if($_SERVER['REQUEST_METHOD'] ==  'GET' ){
		$mid=$_GET["mid"];
		$did=$_GET["did"];
}

if($did != null){
		print "<br><div align=left>
		<input value='Back To Director' type=button onclick='window.location=\"director?id=$did\"'></input><br></div>";
}
else if($mid != null){
		print "<br><div align=left>
				<input value='Back To Movie' type=button onclick='window.location=\"movie?id=$mid\"'></input><br></div>";
}

if($did == null){
	$res=make_query($getDirectorsQuery, $conn);
	$i=0;

	while($row =  mysql_fetch_row($res)){
		$form["directors"][$i]["name"] = $row[0] . " " .$row[1];
		$form["directors"][$i]["id"] = $row[2];
		$form["directors"][$i]["selected"]=false;
		if($data["directors"]!=null){
			for($j=0; $j < sizeof($data["directors"]); $j++){
				if($row[2] == $data["directors"][$j]){
					$form["directors"][$i]["selected"]=true;
					$data["directors_details"][$j]["name"] = $form["directors"][$i]["name"];
					$data["directors_details"][$j]["id"]=$form["directors"][$i]["id"];
				}
			}
		}
		
		$i++;
	}
}
else{
		if(preg_match("/^[0-9]+$/", $did)){
				$query = "SELECT first,last FROM Director WHERE id=". $did.";";
				$res = make_query($query, $conn);
				$row=mysql_fetch_row($res);
				
				if($row == null){
						print "<h1>Director Not Found</h1>
						   <img src='data/filenotfound.png'/><br>
						   <b>The Director with the id $did does not exist! please select another movie.</b>";
						   
						exit();
				}
				else
					print "<h1>Add Directors to Movie</h1><h2>Actor: ".$row[0]." ".$row[1]."</h2>";
				
		}
		else{
			print "<h1>Invalid Director Id</h1>
			   <img src='data/error.png'/><br>
			   <b>The id \"$did\" is not an acceptable id.</b>
			   ";
			exit();
		}

}

if($mid == null){
	$res=make_query($getMoviesQuery, $conn);
	$i=0;
	
	$error=false;
	while($row =  mysql_fetch_row($res)){
		$form["movies"][$i]["title"] = $row[0];
		$form["movies"][$i]["id"] = $row[1];
		$form["movies"][$i]["selected"]=false;
		if($data["movies"]!=null){
			for($j=0; $j < sizeof($data["movies"]); $j++){
				if($row[1] == $data["movies"][$j]){
					$form["movies"][$i]["selected"]=true;
					$data["movies_details"][$j]["title"] = $form["movies"][$i]["title"];
					$data["movies_details"][$j]["id"]=$form["movies"][$i]["id"] = $row[1];
				}
			}
		}
		$i++;
	}
	
}
else{
		if(preg_match("/^[0-9]+$/", $mid)){
				$query = "SELECT title, year FROM Movie WHERE id=". $mid.";";
				$res = make_query($query, $conn);
				$row=mysql_fetch_row($res);
				if($row == null){
						print "<h1>Movie Not Found</h1>
						   <img src='data/filenotfound.png'/><br>
						   <b>The Movie with the id $mid does not exist! please select another movie.</b>";
						exit();
				}
				else
					print "<h1>Add Movies to Director Resume</h1><h2>Movie: ".$row[0]."(".$row[1].")</h2>";
				
		}
		else{
			print "<h1>Invalid Movie Id</h1>
			   <img src='data/error.png'/><br>
			   <b>The id \"$mid\" is not an acceptable id.</b>
			   ";
			exit();
		}
}

		print"<form action=addmoviedirector.php method=post>
		<table align=center><tr><td><b>";
		if($did == null && $mid != null){
				print "<h1>Choose Directors</h1>";
				print "<input type=hidden readonly=\"readonly\" name=mid value=".$mid . " />";
				
		}
		else if($mid == null && $did != null){
				print "<h1>Choose Movies</h1>";
				print "<input type=hidden readonly=\"readonly\" name=did value=".$did . " />";
		}
		else if($mid == null && $did == null){
				print "<h1>Choose Movie/Director Pair</h1>";
		}
		print "</b></td>
		</tr>
		<tr>
		<td>";
	
		if($did == null && $mid != null){
			print "<select  name='directors[]' multiple=true size=10>";
			for($i =0; $i < sizeof($form["directors"]); $i++){
					
					if($form["directors"][$i]["selected"]==false)
						print "<option value=\"".$form["directors"][$i]["id"]."\">".$form["directors"][$i]["name"]."</option>";
					else
						print "<option selected=true value=\"".$form["directors"][$i]["id"]."\">".$form["directors"][$i]["name"]."</option>";
					
			}
			print "</select>";
		}
		else if($mid == null && $did != null){
			print "<select  name='movies[]' multiple=true size=10>";
			for($i =0; $i < sizeof($form["movies"]); $i++){
					if($form["movies"][$i]["selected"]==false)
						print "<option value=\"".$form["movies"][$i]["id"]."\">".$form["movies"][$i]["title"]."</option>";
					else
						print "<option selected=true value=\"".$form["movies"][$i]["id"]."\">".$form["movies"][$i]["title"]."</option>";
			}
			print "</select>";
		}
		else if($mid == null && $did == null){
			print "<select  name='movies[]' size=10>";
			for($i =0; $i < sizeof($form["movies"]); $i++){
					if($form["movies"][$i]["selected"]==false)
						print "<option value=\"".$form["movies"][$i]["id"]."\">".$form["movies"][$i]["title"]."</option>";
					else
						print "<option selected=true value=\"".$form["movies"][$i]["id"]."\">".$form["movies"][$i]["title"]."</option>";
			}
			print "</select></td><td>";
			print "<select  name='directors[]' size=10>";
			for($i =0; $i < sizeof($form["directors"]); $i++){
					
					if($form["directors"][$i]["selected"]==false)
						print "<option value=\"".$form["directors"][$i]["id"]."\">".$form["directors"][$i]["name"]."</option>";
					else
						print "<option selected=true value=\"".$form["directors"][$i]["id"]."\">".$form["directors"][$i]["name"]."</option>";
					
			}
			print "</select>";
		}
		
		print "
		</select>
		</td>
		<td><table  align=center id=rolelist>
		";
		
		
		print "</table>
		</td>
		</tr>
		<tr>
		<td></td>";
		if($mid ==null && $did == null)
			print "<td></td>";
			
		print "
		</tr>
		</table>
		<br>
		<br>
		<br>
		<input type=submit  name=AddRole value='Add Director'></input></form><br>
		</form>";
		
		

		if(isset($_POST["AddRole"])){
			if($error ==false){
				
				if($did == null && $mid != null){
					for($i=0; $i < sizeof($data["directors"]); $i++){
						$insertquery = "INSERT INTO MovieDirector (mid, did) VALUES(".
							$mid."," . $data["directors_details"][$i]["id"] . ")";
						
						
						$existsquery="SELECT * FROM MovieDirector WHERE mid=".
							$mid." AND did=" . $data["directors_details"][$i]["id"].";" ;
						
						$row=mysql_fetch_row(make_query($existsquery, $conn));
						if($row != null){
							print "<font color=red>Failed: Director  ".
									$data["directors_details"][$i]["name"]." already directs movie.</font><br>";
						}
						else{
							make_query($insertquery, $conn);
							print "<font color=green>Added Director <b>".$data["directors_details"][$i]["name"]."</b>
										to Movie.</font><br>";
							
						}
					}
					
					
				}
				if($mid == null && $did != null){
					for($i=0; $i < sizeof($data["movies"]); $i++){
						$insertquery = "INSERT INTO MovieDirector (mid, did) VALUES(".
						$data["movies_details"][$i]["id"]."," . $did .")";
						
						$existsquery="SELECT * FROM MovieDirector WHERE mid=".
							$data["movies_details"][$i]["id"]. " AND did=" . $did .";" ;
						
						$row=mysql_fetch_row(make_query($existsquery, $conn));
						if($row != null){
							print "<font color=red>Failed: Director  already directs movie <b>".
									$data["movies_details"][$i]["title"]."</b></font><br>";
						}
						else{
							make_query($insertquery, $conn);
							print "<font color=green>Added Movie <b>".$data["movies_details"][$i]["title"]."</b>
										to list of directed movies</font><br>";
							
						}
						
					}
					
					
				}
				if($mid == null && $did == null){
					if(sizeof($data["movies"]) != sizeof($data["directors"])){
						print "<font color=red>Failed: Please select both a Movie and a Director</font><br>";
						exit();
					}
					
					for($i=0; $i < sizeof($data["movies"]); $i++){
						$insertquery = "INSERT INTO MovieDirector (mid, did) VALUES(".
							$data["movies_details"][$i]["id"]."," . $data["directors_details"][$i]["id"] .")";
						
						$existsquery="SELECT * FROM MovieDirector WHERE mid=".
						$data["movies_details"][$i]["id"].
						" AND did=" . $data["directors_details"][$i]["id"].";" ;
								
						$row=mysql_fetch_row(make_query($existsquery, $conn));
						if($row != null){
							print "<font color=red>Failed: Director <b>".$data["directors_details"][$i]["name"]
										."</b> already directs movie <b>".$data["movies_details"][$i]["title"]."</b></font><br>";
						}
						else{
							make_query($insertquery, $conn);
							print "<font color=green>Added Director <b>".$data["directors_details"][$i]["name"]
										."</b> to movie <b>".$data["movies_details"][$i]["title"]."</b></font><br>";
						}
						
					}
				}
				
				
				
			}
			else{
				print "There was an error in the form. Please fix the error.<br>";
				
			}
			print "</div>";
		}
		






?>

<br>
</body>
</html>

